//
//  SixIconView.m
//  IconMaster
//
//  Created by mac on 2017/7/25.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "SixIconView.h"

@implementation SixIconView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //
        [self setUI];
    }
    return self;
}
- (void)setUI{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelView)];
    [self addGestureRecognizer:tap];
    
    UIImageView *whiteView_back = [[UIImageView alloc] initWithFrame:CGRectMake(0, KHeight - 250, KWidth, 250)];
    whiteView_back.image = [UIImage imageNamed:@"bottompop"];
    [self addSubview:whiteView_back];
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, KHeight - 220, KWidth, 220)];
    whiteView.backgroundColor = [UIColor clearColor];
    [self addSubview:whiteView];
    
    CGFloat w = 0;//保存前一个button的宽以及前一个button距离屏幕边缘的距离
    CGFloat h = 0;//用来控制button距离父视图的高
    NSInteger clearance = 10;
    CGFloat wide = (KWidth - clearance * 5) / 3;
    
    self.titleArr = @[@{@"image":@"telephone",@"title":@"hightelephone"},@{@"image":@"shortmessage",@"title":@"highshortmessage"},@{@"image":@"mailbox",@"title":@"highmailbox"},@{@"image":@"app",@"title":@"highapp"},@{@"image":@"webpage",@"title":@"highwebpage"},@{@"image":@"personalTailor",@"title":@"highpersonalTailor"}];
    for (int i = 0; i < self.titleArr.count; i++) {
        self.sixButton = [[UIButton alloc] init];
        [self.sixButton addTarget:self action:@selector(handleClick:) forControlEvents:UIControlEventTouchUpInside];
        self.sixButton.tag = i;
        self.sixButton.frame = CGRectMake(clearance +w, h, wide, 80);
        if (clearance + w + wide > KWidth) {
            w = 0;
            h = h + self.sixButton.frame.size.height + 10;//距离父视图也变化
            self.sixButton.frame = CGRectMake(10 + w, h, wide , 80);//重设button的
        }
        [self.sixButton setImage:[UIImage imageNamed:self.titleArr[i][@"image"]] forState:Normal];
//        [self.sixButton setImage:[UIImage imageNamed:self.titleArr[i][@"title"]] forState:UIControlStateHighlighted];
//        [self.sixButton setTitle:self.titleArr[i][@"title"] forState:Normal];
        self.sixButton.imageEdgeInsets = UIEdgeInsetsMake(5,13,20,self.sixButton.titleLabel.bounds.size.width);
        self.sixButton.titleEdgeInsets = UIEdgeInsetsMake(60, -self.sixButton.titleLabel.bounds.size.width-50, 0, 0);
        [self.sixButton setTitleColor:[UIColor blackColor] forState:Normal];
        self.sixButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        //        self.shareButton.backgroundColor = [UIColor grayColor];
        w = self.sixButton.frame.size.width + self.sixButton.frame.origin.x;
        [whiteView addSubview:self.sixButton];
    }
}
- (void)handleClick:(UIButton *)btn{
   
    NSString *contentString = [NSString string];
    switch (btn.tag) {
        case 0:
            contentString = Telephone;
            break;
        case 1:
            contentString = SmsMessage;
            break;
        case 2:
            contentString = Email;
            break;
        case 3:
            contentString = AppEdite;
            break;
        case 4:
            contentString = WebSite;
            break;
        case 5:
            contentString = PriButton;
            break;   
        default:
            break;
    }
        if ([self.delegate respondsToSelector:@selector(sixIconViewButton:)]) {
            [self.delegate sixIconViewButton:contentString];
        }
  
    
}
-(void)cancelView{
    [self removeFromSuperview];
    [AdAndHint loadInterstitialIfNeed];
}
@end
